# 此接口用于重新计算每个study的slice数量并更新

from application import db
from pyorthanc import find, Orthanc
orthanc = Orthanc('http://localhost:8042',
                  username='orthanc', password='orthanc')

from Flask_run import db
from database.models.mri import Mri
from database.models.patient import Patient
from database.models.study_id import Study_id





def upload_slice():

    patients = find(
            orthanc=orthanc,
            study_filter=lambda s: s.id_ == ort_study_id
        )
    for patient in patients:
        # dic=patient.get_main_information()
        for study in patient.studies:
            slice_count=0
            for series in study.series:
                series_info=series.get_main_information()
                slice_num=len(series_info['Instances'])
                slice_count+=slice_num
                # 上传slice到mysql
                Mri.query.filter(Mri.study_id == study_instance_uid).update({'slice': slice_count})
                # 提交会话
    db.session.commit()    